﻿using DBModels;
using Method.DAL;
using Method.DateTime;
using Models;
using Models.VModels;
using RedisHelper.User;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;

namespace Api.WDS.Methods
{
    public class UserDetailsMet
    {
        public object StackExchangeRedisHelper { get; private set; }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public V_UserInfo GetUserInfo(int uid)
        {
            V_UserInfo v_UserInfo = new V_UserInfo();

            string avatar = null, nickName = null;
            if (!new GetUserInfo().GetAvatarAndNickNameByUserId(uid, ref avatar, ref nickName))
            {
                return null;
            }
            v_UserInfo.Avatar = StaticConfigPath.AvatarPath(avatar);
            v_UserInfo.NickName = nickName;

            UserStatistics us = new UserStatistics();
            v_UserInfo.PagCount = us.GetPayCount(uid);
            //用户发表文章数量需要从数据库读,暂时搁置
            v_UserInfo._PayCount = us.Get_PayCount(uid);

            return v_UserInfo;
        }

        /// <summary>
        /// 修改用户昵称
        /// </summary>
        /// <param name="token"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public bool UpdateNickName(int uid, string newName)
        {
            using (ArticeContext context = new ArticeContext())
            {
                var data = context.UserInfos.FirstOrDefault(u => u.Id == uid);
                if (data is null)
                    return false;
                using (var tran = context.Database.BeginTransaction())
                {
                    try
                    {
                        data.NickName = newName;
                        context.Entry(data).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        if (context.SaveChanges() == 1)
                        {
                            //更新缓存
                            if (new RedisHelper.User.UserInfo().InsertUserInfo(uid, new Models.RModels.R_UserInfo()
                            {
                                Avatar = data.Avatar,
                                NickName = newName,
                                Level = context.Accounts.FirstOrDefault(u => u.Id == uid).Level
                            }))
                            {
                                //更新成功
                                tran.Commit();
                                return true;
                            }
                            else
                            {
                                //更新失败,回滚
                                tran.Rollback();
                                return false;
                            }
                        }
                        return false;
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        return false;
                    }
                }
            }

        }

        /// <summary>
        /// 获取用户关注列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<V_UserPay> GetUserPayList(int uid)
        {
            List<V_UserPay> list = new List<V_UserPay>();
            var listR = new UserStatistics().GetPayList(uid);
            using (ArticeContext context = new ArticeContext())
            {
                listR.ForEach(u =>
                {
                    var userInfo = new GetUserInfoByUid().GetUserInfo(u.UserId);
                    if (userInfo != null)
                    {
                        var us = new UserStatistics();
                        list.Add(new V_UserPay()
                        {
                            UserId = u.UserId,
                            NiceName = userInfo.NickName,
                            Avatar = StaticConfigPath.AvatarPath(userInfo.Avatar),
                            Level = userInfo.Level,
                            PayPassiveCount = us.Get_PayCount(u.UserId),
                            PayCount = us.GetPayCount(u.UserId),
                            Time = GetPastTime.getTime(u.Time),
                            IsMutual = us.GetMutualPay(u.UserId, uid)
                        });
                    }
                });
            }
            return list;
        }

        /// <summary>
        /// 获取用户粉丝列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<V_UserPay> GetUser_PayList(int uid)
        {
            List<V_UserPay> list = new List<V_UserPay>();
            var listR = new UserStatistics().Get_PayList(uid);
            using (ArticeContext context = new ArticeContext())
            {
                listR.ForEach(u =>
                {
                    var userInfo = new GetUserInfoByUid().GetUserInfo(u.UserId);
                    if (userInfo != null)
                    {
                        var us = new UserStatistics();
                        list.Add(new V_UserPay()
                        {
                            UserId = u.UserId,
                            NiceName = userInfo.NickName,
                            Avatar = StaticConfigPath.AvatarPath(userInfo.Avatar),
                            Level = userInfo.Level,
                            PayPassiveCount = us.Get_PayCount(u.UserId),
                            PayCount = us.GetPayCount(u.UserId),
                            Time = GetPastTime.getTime(u.Time),
                            IsMutual = us.GetMutualPay(uid,u.UserId)
                        });
                    }
                });
            }
            return list;
        }

        /// <summary>
        /// 用户关注/取消
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="curUid"></param>
        /// <returns>1:关注成功,2:取消关注成功,3:操作失败</returns>
        public int UserPayCheck(int uid, int curUid)
        {
            UserStatistics us = new UserStatistics();
            //查询该用户是否已经关注
            if (new UserStatistics().GetMutualPay(uid, curUid))
            {
                //已关注,要取消关注
                if (us.UserCancelPay(uid, curUid))
                    return 2;
                return 3;
            }
            else
            {
                //未关注,要关注
                if (us.UserPay(uid, curUid))
                    return 1;
                return 3;
            }
        }
    }
}
